/**
 * 模态框
 */
module.exports = function ($scope, $state, $stateParams,$resource,$modal, toaster) {
	/* 分页
     * ========================================= */
    $scope.maxSize = 5;             //最多显示多少个按钮
    $scope.currentPage = 1;      //当前页码
    $scope.itemsPerPage = 10         //每页显示几条
	


	$scope.pageChanged = function () {
       var para = {
            pageNo:$scope.currentPage,
            pageSize:$scope.itemsPerPage,
        };
		$resource('/api/as/dsc/traffic/findDriverInfoList', {}, {}).
		save({}, function(res){			
				
			if(res.errcode === 0 || res.errcode === 10003){
				$scope.objs=res.data.results;									
				console.log(res);
				$scope.totalItems = res.data.totalRecord;


			}else{
				alert(res.errmsg);
			}
		});
    };
	$scope.pageChanged();

	$scope.b= '0';
	$scope.c = '';
	$scope.show = function(index){
		console.log('进来了');
		console.log(index);
		console.log($scope.b, $scope.c);
		if($scope.b==0){
			$scope.b='1',
			$scope.c=index
		}else if($scope.b==1){
			if($scope.c!=index){
				$scope.c=index
			}else{
				$scope.b='0',
				$scope.c=index
			}	
		}	
	};

	// 搜索
	$scope.search=function(){
	var dic = {
		pageNo:$scope.currentPage,
		pageSize:$scope.itemsPerPage,
		
	}
		
		$resource('', {}, {}).save(dic, function(res){
		if(res.errcode === 0 || res.errcode === 10003){
			// console.log(res);
			$scope.objs=res.data.results;
			$scope.totalItems = res.data.totalRecord;
		}else{
			alert(res.errmsg);
		}
	});
	}

	// 添加
	$scope.adddriver= function (item) {
		 $scope.item = item;
		// console.log($scope.objs[index]);			
		var modalInstance = $modal.open({
			template: require('../views/add_driver.html'),
			controller: 'add_driver',
			size: 'lg',
			resolve: {
				items: function () {
					return $scope.item;
				}
			}
		});
		modalInstance.result.then(function (showResult) {	
				$scope.pageChanged();
		});
	}

	// 删除
	$scope.delete = function(id){
		console.log(id);
		$resource('/api/as/dsc/traffic/deleteDriverInfo', {}, {}).
		save({'id' : id}, function(res){			
			if(res.errcode !== 0){
				toaster.error({title:"",body:res.errmsg});
			}else{
				toaster.success({title:"",body:"删除成功"});
				$scope.pageChanged();
				
			}
		});
	}
	// 编辑
	$scope.edit= function (item) {
		 $scope.item = item;
		var modalInstance = $modal.open({
			template: require('../views/add_driver.html'),
			controller: 'add_driver',
			size: 'lg',
			resolve: {
				items: function () {
					return $scope.item;
				}
			}
		});
		modalInstance.result.then(function (showResult) {	
				$scope.pageChanged();
		});
	}

}